#include "totaldataprocess.h"

TotalDataProcess::TotalDataProcess()
{
    myDB=new ProjectDB();
    m_sTestType="I";
    nowBfd=1;
}
void TotalDataProcess::init()
{
    m_iBiJiaoCiShu=10;
    m_iTestTimes=0;
    testPointOver=false;
    testOver=false;
    m_CyFlag=false;

    memset(m_dblBc,0,sizeof(m_dblBc));
    memset(m_dblJc,0,sizeof(m_dblJc));


}
void TotalDataProcess::setTestType(QString stype,QString jd)
{
    m_sTestType=stype;
    m_sJD=jd;
    getTestInfo();
}
void TotalDataProcess::getTestInfo()
{
    QString sql="select * from testdata where ";
    sql += "testtype='" + m_sTestType + "'";
    sql += "and testlevel='" + m_sJD + "'";
    tabTestData= myDB->openWcxxforSql(sql);
    if (tabTestData.next())
    {
        nowBfd=tabTestData.value(2).toInt();
    }
    sql = "select * from errorpoint where ";
    sql +="pointvalue="+ QString::number(nowBfd);
    tabErrorPoint= myDB->openWcxxforSql(sql);
    if (tabErrorPoint.next())
    {
        nowtestBfderror=tabErrorPoint.value(1).toFloat();
    }
}

bool TotalDataProcess::ProcessData(double bfd)
{
    bool isOK=false;
    float tempminbfd=nowBfd-nowtestBfderror;
    float tempmaxbfd=nowBfd+nowtestBfderror;
    if ((bfd>tempminbfd)&&(bfd<tempmaxbfd))
    {
        isOK=true;
        if (!m_CyFlag)
            return isOK;
        m_iTestTimes++;
        if (m_iTestTimes==m_iBiJiaoCiShu)
        {
            m_iTestTimes=0;
            testPointOver=true;
            if (tabTestData.next())
            {
                nowBfd=tabTestData.value(2).toInt();
            }
            else
            {
                testOver=true;
            }
            tabErrorPoint.clear();
            QString sql = "select * from errorpoint where ";
            sql +="pointvalue="+ QString::number(nowBfd);
            tabErrorPoint= myDB->openWcxxforSql(sql);
            if (tabErrorPoint.next())
            {
                nowtestBfderror=tabErrorPoint.value(1).toFloat();
            }
            tabErrorPoint.clear();
        }
    }
    return isOK;
}
double TotalDataProcess::SZLB(int num,double*D_ADD)//num组double类型的数，去掉一个最大一个最小，再取平均
{
        int i;
        double Max_Data;
        double Min_Data;
        double Result;

        Max_Data=Min_Data=Result=*D_ADD;

        for(i=1;i<num;i++)
        {
            Result+=(*(D_ADD+i));
            if(*(D_ADD+i)<Min_Data)
            {
                Min_Data=*(D_ADD+i);
            }
            if(*(D_ADD+i)>Max_Data)
            {
                Max_Data=*(D_ADD+i);
            }
        }
        if(num<=2)
            Result=(Result/num);	//如果num<=3,则num个数直接取平均
        else
            Result=(Result-Max_Data-Min_Data)/(num-2);//num>3,num个数去掉一个最大一个最小，再取平均
        return(Result);
}
